home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / os2 / adaptor.zip / ADAPT.ZIP / adaptor / src / xreadint.c < prev    next >
Text File  |  1993-12-05  |  10KB  |  275 lines

  1. /**************************************************************************
  2. *                                                                         *
  3. *  Author      : Werner Meurer, GMD, I1.HR                                *
  4. *  Date        : Jan 93                                                   *
  5. *  Last Update :                                                          *
  6. *                                                                         *
  7. *  Module      : xreadint                                                 *
  8. *                                                                         *
  9. *  Function    : Athena Widget for Input natural number                   *
  10. *                                                                         *
  11. *  Export :                                                               *
  12. *                                                                         *
  13. *  void open_integer_input_window(w,client_data,call_data)                *
  14. *                                                                         *
  15. **************************************************************************/
  16.  
  17. #include "xglobal.h"
  18. #define _ABS
  19. #include <math.h>
  20. #include "xoptions.h"
  21. #include "xhelp.h"
  22. #include "xreadint.h" /* variables and callbackprocedure */
  23.  
  24. #define DEBUG(x)
  25.  
  26. Widget sshell;
  27.  
  28. Widget intdialog,intdone,inputint;
  29.  
  30. /************************************************************************/
  31. /* reverse()        */
  32. /* INPUT: string is an array of char    */
  33. /* OUTPUT: the reverse string     */
  34. /************************************************************************/
  35. void reverse(string)
  36. char *string;
  37. {
  38.        int length = strlen(string);
  39.        int i;
  40. DEBUG(printf("reverse\n"));
  41.        for (i=0;i<(int) (length /2);i++)
  42.        {
  43.         int dummy;
  44.         dummy = string[length-1-i];
  45.         string[length-1-i] = string[i];
  46.         string[i] = dummy;
  47.        }
  48.        string[length] = '\0';
  49. }
  50.  
  51. /************************************************************************/
  52. /* itoa()        */
  53. /* INPUT: number is an integer value    */
  54. /*      string is an allocated string (array of char)  */
  55. /* OUTPUT: in the string is the number    */
  56. /* return value: string      */
  57. /* the procedure converts a number to a string    */
  58. /************************************************************************/
  59. char *itoa(number,string)
  60. int number;
  61. char *string;
  62. {
  63.        int i = 0;
  64. DEBUG(printf("itoa\n"));
  65.        while (number > 0)
  66.        {
  67.         string[i] = number % 10;
  68.         number = (int) (number / 10);
  69.         string[i] = string[i] + '0';
  70.         i++;
  71.        }
  72.        string[i] = '\0';
  73.        reverse(string);
  74.        return(string);
  75. }
  76.  
  77. /************************************************************************/
  78. /* ArrayChoosed()       */
  79. /* INPUT: w is the widget which called the procedure  */
  80. /*      client_data is the user defined data (parameter) */
  81. /*      call_data is x-window defined data   */
  82. /* OUTPUT: failure messages in the message window   */
  83. /* Translates the String from the TextWidget into an integer   */
  84. /* ArraySize        */
  85. /************************************************************************/
  86. void ArrayChoosed (w,client_data,call_data)
  87. Widget w;
  88. XtPointer client_data;
  89. XtPointer call_data;
  90. {
  91.     Arg args[10];
  92.     int n = 0;
  93.     char *string;
  94.     int letternumber;
  95.     Widget caller = (Widget) client_data;
  96.     char name[MAXLENGTH];
  97.        /* Static Array is choosed    */
  98.        /* Get and check the String from the widget  */
  99. DEBUG(printf("ArrayChoosed\n"));
  100.     n = 0;
  101.     strcpy(name,XtName(XtParent(XtParent(w))));
  102.     XtSetArg(args[n],XtNstring,&string);n++;
  103.     XtGetValues(caller,args,n);
  104.     letternumber = strlen(string);
  105.     /* check if there are only digits or not */
  106.     {
  107.        int i = 0;
  108.        while (isdigit(string[i]))
  109.        {
  110.         i++;
  111.        }
  112.        if (i == letternumber) /* only digits */
  113.        {
  114.            if (letternumber > (int)(log10((double)MAXSIZE)))
  115.            {
  116.         strcpy(last_message,"number too big, MAXSIZE choosed");
  117.         set_message();
  118.         sshell = 0;
  119.         XtPopdown(XtParent(XtParent(w)));
  120.         integer_init_or_writeback(name,MAXSIZE,WRITEBACK);
  121.         return;
  122.            }
  123.            else
  124.            {
  125.         int number;
  126.         sshell = 0;
  127.         string[i] = '\0';
  128.         XtPopdown(XtParent(XtParent(w)));
  129.         number = atoi(string);
  130.         if (number > MAXSIZE)
  131.         {
  132.          strcpy(last_message,"number too big, MAXSIZE choosed");
  133.          set_message();
  134.          integer_init_or_writeback(name,MAXSIZE,WRITEBACK);
  135.          return;
  136.         }
  137.         else
  138.         {
  139.          integer_init_or_writeback(name,number,WRITEBACK);
  140.          return;
  141.         }
  142.            }
  143.        }
  144.        strcpy(last_message,"Only positive integers are allowed");
  145.        set_message();
  146.     }
  147. }
  148.  
  149. /************************************************************************/
  150. /* return_key()        */
  151. /* INPUT: w is the Widget in which the event happened  */
  152. /*      event is the pointer to the eventstructure  */
  153. /* OUTPUT: none       */
  154. /* The Procedure simulates the callback from the Accept button  */
  155. /************************************************************************/
  156. void return_key(w,event)
  157. Widget w;
  158. XEvent *event;
  159. {
  160.        DEBUG(printf("return_key\n"));
  161.        ArrayChoosed(w,(XtPointer) w,(XtPointer) 0);
  162. }
  163.  
  164. /************************************************************************/
  165. /* is_no_digit_or_return()      */
  166. /* INPUT: w is the Widget in which the event happened  */
  167. /*      event is the pointer to the eventstructure  */
  168. /* OUTPUT: failure message in the message window   */
  169. /************************************************************************/
  170. void is_no_digit_or_return(w,event)
  171. Widget w;
  172. XEvent *event;
  173. {
  174. DEBUG(printf("is_no_digit_or_return\n"));
  175.        strcpy(last_message,"Only digits or return is allowd");
  176.        set_message();
  177. }
  178.  
  179. /************************************************************************/
  180. /* open_integer_input_window()      */
  181. /* INPUT: w is the widget which called the procedure  */
  182. /*      client_data is the user defined data (parameter) */
  183. /*      call_data is x-window defined data   */
  184. /* OUTPUT: none       */
  185. /* The procedure opens the windows for an integer input   */
  186. /* REMARK: This procedure is written to use again. It is only */
  187. /*      opened, if it is not open, no second window of this */
  188. /*      procedure will be opened.    */
  189. /*      Only the Widgets must be global (sshell,intdialog, */
  190. /*      intdone,inputint)     */
  191. /************************************************************************/
  192. void open_integer_input_window(w,client_data,call_data)
  193. Widget w;
  194. XtPointer client_data;
  195. XtPointer call_data ;
  196. {
  197.        char *name;
  198.        Widget parent;
  199.        Widget poswidget = w;
  200.        Arg args[10];
  201.        int n = 0;
  202.        char string[MAXLENGTH];
  203.        Position x, y;
  204.        Dimension width, height;
  205.        name = XtName (w);
  206. DEBUG(printf("open_integer_input_window\n"));
  207.  
  208.        if (sshell != 0)
  209.        {
  210.         strcpy(last_message,"Window is open");
  211.         set_message();
  212.         return;
  213.        }
  214.         sshell = XtVaCreatePopupShell(
  215.               name, /* same name as calling widget  */
  216.               transientShellWidgetClass,
  217.               XtParent (w),
  218.               NULL);              /* terminate varargs list */
  219.    /* * get the coordinates of the middle of poswidget widget.  */
  220.  
  221.        XtVaGetValues(poswidget,
  222.               XtNwidth, &width,
  223.               XtNheight, &height,
  224.               NULL);
  225.  
  226.    /* translate coordinates in application top-level window
  227.     * into coordinates from root window origin.               */
  228.  
  229.         XtTranslateCoords(poswidget,        /* Widget */
  230.            (Position) width/2,        /* x */
  231.            (Position) height/2,       /* y */
  232.            &x, &y);          /* coords on root window */
  233.  
  234.    /* move popup shell to this position (it's not visible yet) */
  235.  
  236.        XtVaSetValues(sshell,
  237.                    XtNx, x,
  238.                    XtNy, y,
  239.                    NULL);
  240.        intdialog = XtVaCreateManagedWidget(
  241.               "intdialog",            /* widget name   */
  242.               formWidgetClass,        /* widget class */
  243.               sshell,                 /* parent widget*/
  244.               NULL);                  /* terminate varargs list */
  245.  
  246.        intdone = XtVaCreateManagedWidget(
  247.           "Accept",                      /* widget name   */
  248.           commandWidgetClass,             /* widget class */
  249.           intdialog,                      /* parent widget*/
  250.           NULL);              /* terminate varargs list */
  251.  
  252. /* set the arguments for the text widget    */
  253.        n = 0;
  254.        XtSetArg(args[n],XtNwidth,200);n++;
  255.        XtSetArg(args[n],XtNheight,30);n++;
  256.        XtSetArg(args[n],XtNfromVert,intdone);n++;
  257.        itoa(integer_init_or_writeback(name,strlen(name),INIT),string);
  258.        XtSetArg(args[n],XtNstring,string);n++;
  259.        XtSetArg(args[n],XtNeditType,"edit");n++;
  260.        inputint = XtCreateManagedWidget ("Input", asciiTextWidgetClass,
  261.                           intdialog, args, n);
  262.  
  263. /* make the new translationtables known     */
  264.  
  265.         make_help_available (inputint);
  266.  
  267.        XtOverrideTranslations(inputint,
  268.         XtParseTranslationTable(KeyTranslation));
  269.  
  270.         XtAddCallback (intdone, XtNcallback,ArrayChoosed, inputint);
  271.         XtPopup(sshell,XtGrabNone);
  272. }
  273.  
  274.  
  275.